package galaxies;

import galaxies.enums.EGalaxyType;

import java.util.ArrayList;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SpiralStarLocationGenerator {
	
	private static final Logger logger = Logger.getLogger(SpiralStarLocationGenerator.class.getName());
	
	public ArrayList<ArrayList<Integer>> chooseRandomLocations(
			ArrayList<ArrayList<Integer>> primaryLocations, 
			ArrayList<ArrayList<Integer>> secondaryLocations,
			ArrayList<ArrayList<Integer>> tertiaryLocations, 
			double primaryFract, double secondaryFract, double tertiaryFract,			
			int density,
			EGalaxyType type) {
		
		ArrayList<ArrayList<Integer>> starLocations = new ArrayList<ArrayList<Integer>>();
		
		int totalLocations = primaryLocations.size() + secondaryLocations.size() + tertiaryLocations.size();
		
//		int totalLocations = (int) (primaryLocations.size()*primaryFract + 
//			secondaryLocations.size()*secondaryFract + 
//			tertiaryLocations.size()*tertiaryFract);
		
		logger.log(Level.INFO, "Shuffling arrays...");
		Collections.shuffle(primaryLocations);
		Collections.shuffle(secondaryLocations);
		Collections.shuffle(tertiaryLocations);
		logger.log(Level.INFO, "Finished shuffling");
		
		/*
		 * Given galaxy size, totalLocations, and galaxy density, calculate number of stars.
		 * Galaxy generation algorithm may create room for greater or fewer stars than originally calculated,
		 * especially when generating spiral galaxies where many new branches could be created 
		 */
//		int locationsPerStar = 0;
//		if (type.equals(EGalaxyType.ELLIPTICAL)) {
//			locationsPerStar = density.getEllipticalTiles();
//		}
//		else if (type.equals(EGalaxyType.SPIRAL)) {
//			locationsPerStar = density.getSpiralTiles(); 
//		}
//		else if (type.equals(EGalaxyType.BAR_SPIRAL)) {
//			locationsPerStar = density.getBarSpiralTiles();
//		}
//		else if (type.equals(EGalaxyType.UNIFORM)) {
//			locationsPerStar = density.getUniformTiles();
//		}
//		else {
//			locationsPerStar = density.getEllipticalTiles();
//		}
		
		int numStars = (int) ((double) totalLocations/ ((double) density));
		
		//minimum number of stars in the smallest galaxy
		if (numStars < 10) {
			numStars = 10;
		}
		else if (numStars > 10000) { //max number in the largest galaxy
			numStars = 10000;
		}
		
		logger.log(Level.INFO, "GalaxyType: " + type.toString());
		logger.log(Level.INFO, "Density: " + density);
		logger.log(Level.INFO, "NumStars: " + numStars);
		
		logger.log(Level.INFO, "Selecting Locations");
		for (int i = 0; i < primaryFract*numStars; i++) {
			starLocations.add(primaryLocations.get(i));
		}
		
		for (int j = 0; j < secondaryFract*numStars; j++) {
			starLocations.add(secondaryLocations.get(j));
		}
		
		for (int k = 0; k < tertiaryFract*numStars; k++) {
			starLocations.add(tertiaryLocations.get(k));
		}
		
		logger.log(Level.INFO, "Finished selecting locations");
		
		return starLocations;
	}

}
